package org.apache.rocketmq.store;

import org.apache.rocketmq.common.constant.PermName;
import org.apache.rocketmq.common.message.MessageExtBrokerInner;
import org.apache.rocketmq.store.config.MessageStoreConfig;
import org.apache.rocketmq.store.hook.PutMessageHook;
import org.apache.rocketmq.store.hook.SendMessageBackHook;
import org.apache.rocketmq.store.logfile.MappedFile;
import org.apache.rocketmq.store.queue.ConsumeQueueInterface;
import org.apache.rocketmq.store.stats.BrokerStatsManager;
import org.rocksdb.RocksDBException;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.CompletableFuture;


public interface MessageStore {

    boolean load();

    void start() throws Exception;

    void shutdown();

    long dispatchBehindBytes();

    void destroy();

    long getStateMachineVersion();

    List<PutMessageHook> getPutMessageHookList();

    void setSendMessageBackHook(SendMessageBackHook sendMessageBackHook);

    SendMessageBackHook getSendMessageBackHook();

    boolean isShutdown();

    RunningFlags getRunningFlags();

    BrokerStatsManager getBrokerStatsManager();

    boolean isOSPageCacheBusy();

    long now();

    boolean isTransientStorePoolDeficient();

    MessageStoreConfig getMessageStoreConfig();

    default CompletableFuture<PutMessageResult> asyncPutMessage(final MessageExtBrokerInner msg) {
        return CompletableFuture.completedFuture(putMessage(msg));
    }

    PutMessageResult putMessage(final MessageExtBrokerInner msg);

    CommitLog getCommitLog();

    StoreStatsService getStoreStatsService();

    long getMasterFlushedOffset();

    void setMasterFlushedOffset(long masterFlushedOffset);

    void assignOffset(MessageExtBrokerInner msg) throws RocksDBException;

    void increaseOffset(MessageExtBrokerInner msg, short messageNum);

    ConsumeQueueInterface findConsumeQueue(String topic, int queueId);

    void unlockMappedFile(MappedFile unlockMappedFile);

    TransientStorePool getTransientStorePool();

    AllocateMappedFileService getAllocateMappedFileService();

    void setConfirmOffset(long phyOffset);

    StoreCheckpoint getStoreCheckpoint();

    void onCommitLogAppend(MessageExtBrokerInner msg, AppendMessageResult result, MappedFile commitLogFile);

    long getConfirmOffset();

    DispatchRequest checkMessageAndReturnSize(ByteBuffer byteBuffer, boolean checkCRC,
                                              boolean checkDupInfo, boolean readBody);

    void notifyMessageArriveIfNecessary(DispatchRequest dispatchRequest);

    SelectMappedBufferResult selectOneMessageByOffset(long commitLogOffset);

    long getMinPhyOffset();
}
